<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="2_bTX0cWrOhWZSevyWP2GKbiiDcjstFU49O9l3OR2KA">
  <meta name="msvalidate.01" content="03A2D306E532883F73B68345E8849F4F">
  <meta name="baidu-site-verification" content="code-Ps9rWdUK9L">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">



  <script>
  (function(i,s,o,g,r,a,m){i["DaoVoiceObject"]=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;a.charset="utf-8";m.parentNode.insertBefore(a,m)})(window,document,"script",('https:' == document.location.protocol ? 'https:' : 'http:') + "//widget.daovoice.io/widget/0f81ff2f.js","daovoice")
  daovoice('init', {
      app_id: "2777f17e"
    });
  daovoice('update');
  </script>



<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wangbowen11.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":18,"onmobile":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Python 列表的增删改查 1print(&quot;hello world!&quot;)">
<meta property="og:type" content="article">
<meta property="og:title" content="Python笔记">
<meta property="og:url" content="https://wangbowen11.gitee.io/2021/04/14/Python%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="城府">
<meta property="og:description" content="Python 列表的增删改查 1print(&quot;hello world!&quot;)">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="c:/Users/11063/AppData/Roaming/Typora/typora-user-images/image-20201025145535288.png">
<meta property="article:published_time" content="2021-04-14T04:27:49.000Z">
<meta property="article:modified_time" content="2021-04-14T04:28:45.393Z">
<meta property="article:author" content="王博文">
<meta property="article:tag" content="Pyhton">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="c:/Users/11063/AppData/Roaming/Typora/typora-user-images/image-20201025145535288.png">

<link rel="canonical" href="https://wangbowen11.gitee.io/2021/04/14/Python%E7%AC%94%E8%AE%B0/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Python笔记 | 城府</title>
  


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?083f332b78d125b2f33f1add1042682d";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">城府</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">许嵩 | Vae</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-首页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-关于">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-标签">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-分类">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-归档">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-留言板">

    <a href="/guestbook/" rel="section"><i class="fa fa-comments fa-fw"></i>留言板</a>

  </li>
        <li class="menu-item menu-item-网抑云歌单">

    <a href="/playlist" rel="section"><i class="fa fa-music fa-fw"></i>网抑云歌单</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>
    
    
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2Jvd2VuOTk=" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

    
    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wangbowen11.gitee.io/2021/04/14/Python%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="王博文">
      <meta itemprop="description" content="你走之后 一个夏季熬成一个秋<br>我的书上你的正楷眉清目秀<br>一字一字宣告我们和平分手">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="城府">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Python笔记
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              
    
              <time title="创建时间：2021-04-14 12:27:49 / 修改时间：12:28:45" itemprop="dateCreated datePublished" datetime="2021-04-14T12:27:49+08:00">2021-04-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Python/" itemprop="url" rel="index"><span itemprop="name">Python</span></a>
                </span>
            </span>
    
          
            <span id="/2021/04/14/Python%E7%AC%94%E8%AE%B0/" class="post-meta-item leancloud_visitors" data-flag-title="Python笔记" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/04/14/Python%E7%AC%94%E8%AE%B0/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/04/14/Python%E7%AC%94%E8%AE%B0/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>22k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>20 分钟</span>
            </span>
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <p><strong>Python 列表的增删改查</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">&quot;hello world!&quot;</span>)</span><br></pre></td></tr></table></figure>

<span id="more"></span>

<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">list</span> = [<span class="string">&quot;王博文&quot;</span>, <span class="string">&quot;胡歌&quot;</span>, <span class="string">&quot;苍井空&quot;</span>, <span class="string">&quot;小泽玛利亚&quot;</span>, <span class="string">&quot;吴彦祖&quot;</span>]</span><br><span class="line">td = [<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>, <span class="string">&quot;4&quot;</span>, <span class="string">&quot;2&quot;</span>]</span><br><span class="line">num = [<span class="number">1</span>, <span class="number">6</span>, <span class="number">2</span>, <span class="number">1</span>, <span class="number">9</span>, <span class="number">6</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">9</span>]</span><br><span class="line"><span class="built_in">list</span>.append(<span class="string">&quot;吉吉国王&quot;</span>)  <span class="comment"># 默认在列表末尾添加</span></span><br><span class="line"><span class="built_in">list</span>.insert(<span class="number">2</span>, <span class="string">&quot;唐僧&quot;</span>) <span class="comment"># 添加在索引前面</span></span><br><span class="line"><span class="built_in">list</span>.extend(td)<span class="comment"># 合并两个列表</span></span><br><span class="line"><span class="built_in">list</span>.pop(<span class="number">1</span>)  <span class="comment"># 默认删除最后一个列表值，可以删除特定索引的值</span></span><br><span class="line"><span class="built_in">list</span>.remove(<span class="string">&quot;2&quot;</span>)  <span class="comment"># 移除指定的值</span></span><br><span class="line">dd = <span class="string">&quot;王博文&quot;</span></span><br><span class="line">print(<span class="built_in">list</span>.index(dd))  <span class="comment"># 查询在列表中的索引</span></span><br><span class="line">print(<span class="built_in">list</span>.count(<span class="string">&quot;王博文&quot;</span>))  <span class="comment"># 统计在列表出现多少次</span></span><br><span class="line">num.sort()  <span class="comment"># 排序</span></span><br><span class="line"><span class="built_in">list</span>.reverse()<span class="comment"># 反转反转</span></span><br><span class="line"><span class="built_in">list</span>.copy()  <span class="comment"># copy列表</span></span><br><span class="line"><span class="built_in">list</span>.clear()  <span class="comment"># 清空列表</span></span><br><span class="line">a = [<span class="string">&quot;苍井空&quot;</span>, <span class="string">&quot;武藤兰&quot;</span>]</span><br><span class="line">s = <span class="built_in">enumerate</span>(a)  <span class="comment"># 带下标的遍历</span></span><br><span class="line"><span class="keyword">for</span> i, k <span class="keyword">in</span> s:</span><br><span class="line">    print(i, k)</span><br></pre></td></tr></table></figure>

<h3 id="Python-字典的增删改查"><a href="#Python-字典的增删改查" class="headerlink" title="Python 字典的增删改查"></a><strong>Python 字典的增删改查</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">dict</span> = &#123;<span class="string">&quot;年龄&quot;</span>: <span class="number">33</span>, <span class="string">&quot;姓名&quot;</span>: <span class="string">&quot;苍井空&quot;</span>, <span class="string">&quot;身高&quot;</span>: <span class="string">&quot;156&quot;</span>, <span class="string">&quot;籍贯&quot;</span>: <span class="string">&quot;东京&quot;</span>&#125;</span><br><span class="line">dict2 = &#123;<span class="string">&quot;name&quot;</span>: <span class="string">&quot;hge&quot;</span>&#125;</span><br><span class="line">s = <span class="built_in">dict</span>[<span class="string">&quot;姓名&quot;</span>]  <span class="comment"># 查看所对应的值</span></span><br><span class="line"><span class="built_in">dict</span>[<span class="string">&quot;籍贯&quot;</span>] = <span class="string">&quot;世界&quot;</span>  <span class="comment"># 更改字典</span></span><br><span class="line"><span class="comment"># del dict[&quot;年龄&quot;]  # 删除字典</span></span><br><span class="line"><span class="keyword">if</span> <span class="string">&quot;身高&quot;</span> <span class="keyword">in</span> <span class="built_in">dict</span>:  <span class="comment"># 查看字典是否在</span></span><br><span class="line">    print(<span class="string">&quot;在&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">&quot;不在&quot;</span>)</span><br><span class="line">print(<span class="built_in">dict</span>.pop(<span class="string">&quot;年龄&quot;</span>))  <span class="comment"># 返回删除的值</span></span><br><span class="line">print(<span class="built_in">dict</span>.items())  <span class="comment"># 以列表返回可遍历的值</span></span><br><span class="line">print(<span class="built_in">dict</span>.popitem())  <span class="comment"># 返回最后一个劲键值对</span></span><br><span class="line">print(<span class="built_in">dict</span>.values())  <span class="comment"># values() 方法返回一个迭代器，可以使用 list() 来转换为列表，列表为字典中的所有值</span></span><br><span class="line">print(<span class="built_in">dict</span>.get((<span class="string">&quot;年龄&quot;</span>), <span class="number">12</span>))  <span class="comment"># get() 函数返回指定键的值，如果键不在字典中返回默认值</span></span><br><span class="line"><span class="built_in">dict</span>.update(dict2)  <span class="comment"># 更新字典</span></span><br><span class="line"><span class="comment"># 关于字典中 get() 和 setdefault() 的区别：</span></span><br><span class="line"><span class="comment"># 主要在于当查找的键值 key 不存在的时候，setdefault()函数会返回默认值并更新字典，添加键值；而 get() 函数只返回默认值，并不改变原字典。</span></span><br><span class="line"><span class="built_in">dict</span>.setdefault(<span class="string">&quot;age&quot;</span>, <span class="number">23</span>)  <span class="comment"># 如果键不已经存在于字典中，将会添加键并将值设为默认值</span></span><br><span class="line">dict3 = <span class="built_in">dict</span>.fromkeys(<span class="built_in">dict</span>, <span class="number">3</span>)</span><br><span class="line">x = (<span class="string">&quot;key1&quot;</span>, <span class="string">&quot;key2&quot;</span>, <span class="string">&quot;key3&quot;</span>)</span><br><span class="line">this = <span class="built_in">dict</span>.fromkeys(x, <span class="number">3</span>)</span><br><span class="line">print(this)</span><br></pre></td></tr></table></figure>

<h3 id="Python-字符串操作"><a href="#Python-字符串操作" class="headerlink" title="Python 字符串操作"></a><strong>Python 字符串操作</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&quot;hello world&quot;</span></span><br><span class="line">b = <span class="string">&quot;四&quot;</span></span><br><span class="line">b1 = <span class="string">&quot;Ⅵ&quot;</span></span><br><span class="line"><span class="comment"># 定义一个字符串为Unicode，只需要在字符串前添加 &#x27;u&#x27; 前缀即可</span></span><br><span class="line">c = <span class="string">u&quot;112345&quot;</span></span><br><span class="line">d = <span class="string">&quot;     wang    &quot;</span></span><br><span class="line">print(a.ljust(<span class="number">30</span>, <span class="string">&quot;*&quot;</span>))  <span class="comment"># 右边填充30个字符长度</span></span><br><span class="line">print(a.center(<span class="number">30</span>, <span class="string">&quot;*&quot;</span>))  <span class="comment"># 两边各自填充15个字符长度</span></span><br><span class="line">print(a.rjust(<span class="number">30</span>, <span class="string">&quot;*&quot;</span>))  <span class="comment"># 左边填充30个字符长度</span></span><br><span class="line">print(a.isalnum())  <span class="comment"># 检测是否有数字和字母组成不包含符号</span></span><br><span class="line">print(a.islower())  <span class="comment"># 是否全是小写</span></span><br><span class="line">print(a.isalpha())  <span class="comment"># 是否只由字母组成</span></span><br><span class="line">print(a.isdigit())  <span class="comment"># 是否只由数字组成</span></span><br><span class="line">print(b.isnumeric())  <span class="comment"># 可以测试是否由汉语数字组成</span></span><br><span class="line">print(c.isnumeric())  <span class="comment"># 检测字符串是否只由数字组成。这种方法是只针对unicode对象</span></span><br><span class="line">print(a.isspace())  <span class="comment"># 检测字符串是否只由空格组成</span></span><br><span class="line">print(a.istitle())  <span class="comment"># 检测字符串中所有的单词拼写首字母是否为大写，且其他字母为小写</span></span><br><span class="line">print(a.isupper())  <span class="comment"># 检测字符串中所有的字母是否都为大写</span></span><br><span class="line">print(b.join(a))  <span class="comment"># 序列中的元素以指定的字符连接生成一个新的字符串</span></span><br><span class="line">print(d.lstrip())  <span class="comment"># 去掉右边的空字符串，形成新的字符串</span></span><br><span class="line">print(d.rstrip())  <span class="comment"># 去掉左边的空字符串，形成新的字符串</span></span><br><span class="line">print(d.strip())  <span class="comment"># 去掉所有空格</span></span><br><span class="line">print(a.replace(<span class="string">&quot;l&quot;</span>, <span class="string">&quot;w&quot;</span>, <span class="number">2</span>))  <span class="comment"># 替代操作</span></span><br><span class="line"><span class="built_in">str</span> = <span class="string">&quot;welcom to china!&quot;</span></span><br><span class="line">dd = <span class="string">&quot;china!&quot;</span></span><br><span class="line">ff = <span class="string">&quot;wel&quot;</span></span><br><span class="line">print(<span class="built_in">str</span>.endswith(dd))  <span class="comment"># 以china结尾</span></span><br><span class="line">print(<span class="built_in">str</span>.startswith(ff))  <span class="comment"># 以wel开头</span></span><br><span class="line"><span class="comment"># format拆包列表</span></span><br><span class="line">a = [<span class="string">&quot;wa&quot;</span>, <span class="string">&quot;ah&quot;</span>, <span class="string">&quot;ha&quot;</span>]</span><br><span class="line">print(<span class="string">&quot;hell&#123;&#125;,he2&#123;&#125;,he3&#123;&#125;&quot;</span>.<span class="built_in">format</span>(*a))</span><br><span class="line"><span class="comment"># format拆包字典</span></span><br><span class="line">a = &#123;<span class="string">&quot;wang&quot;</span>: <span class="string">&quot;bowen&quot;</span>, <span class="string">&quot;age&quot;</span>: <span class="number">18</span>, <span class="string">&quot;addr&quot;</span>: <span class="string">&quot;henan&quot;</span>&#125;</span><br><span class="line">print(<span class="string">&quot;wojiao:&#123;wang&#125;,zhuzai&#123;addr&#125;,jinnian&#123;age&#125;&quot;</span>.<span class="built_in">format</span>(**a))</span><br><span class="line"><span class="comment"># eval()可以执行字符串中的命令</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-随机数"><a href="#Python-随机数" class="headerlink" title="Python 随机数"></a><strong>Python 随机数</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> random <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"><span class="built_in">list</span> = [<span class="string">&quot;王博文&quot;</span>, <span class="string">&quot;胡歌&quot;</span>, <span class="string">&quot;苍井空&quot;</span>, <span class="string">&quot;小泽玛利亚&quot;</span>, <span class="string">&quot;吴彦祖&quot;</span>]</span><br><span class="line">num = random()  <span class="comment"># 产生 0 到 1 之间的随机浮点数</span></span><br><span class="line">num = randint(<span class="number">1</span>, <span class="number">10</span>)  <span class="comment"># 产生1-10之间的整数</span></span><br><span class="line">num = randrange(<span class="number">1</span>, <span class="number">100</span>, <span class="number">10</span>)  <span class="comment"># 生成从1到100的间隔为10的随机整数</span></span><br><span class="line">num = uniform(<span class="number">1</span>, <span class="number">10</span>)  <span class="comment"># 生成1-10之間的随机小数)</span></span><br><span class="line">number = [<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">4</span>, ]</span><br><span class="line">shuffle(number)  <span class="comment"># 打乱列表顺序</span></span><br><span class="line">x = sample(<span class="built_in">list</span>, k=<span class="number">2</span>)  <span class="comment"># 从list中随机选出两个</span></span><br><span class="line">y = choice(<span class="built_in">list</span>)  <span class="comment"># 选出一个不带符号的值</span></span><br><span class="line">z = choices(<span class="built_in">list</span>, cum_weights=[<span class="number">0</span>, <span class="number">0</span>, <span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>], k=<span class="number">5</span>)</span><br><span class="line"><span class="comment"># 可以设置k值，weightsweights：相对权重 cum_weights：累加权重</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-集合操作"><a href="#Python-集合操作" class="headerlink" title="Python 集合操作"></a><strong>Python 集合操作</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 集合是无序的不重复的</span></span><br><span class="line">jihe = &#123;<span class="string">&quot;吴彦祖&quot;</span>, <span class="string">&quot;小泽玛利亚&quot;</span>, <span class="string">&quot;胡歌&quot;</span>, <span class="string">&quot;古灵&quot;</span>, <span class="string">&quot;武藤兰&quot;</span>, <span class="string">&quot;金庸&quot;</span>, <span class="string">&quot;吴耀祖&quot;</span>, <span class="string">&quot;景天&quot;</span>, <span class="string">&quot;苍井空&quot;</span>&#125;  <span class="comment"># 集合去重</span></span><br><span class="line">jihe2 = &#123;<span class="string">&quot;东皇太一&quot;</span>, <span class="string">&quot;苍井空&quot;</span>, <span class="string">&quot;吴彦祖&quot;</span>, <span class="string">&quot;刘德华&quot;</span>, <span class="string">&quot;鲁班七号&quot;</span>&#125;</span><br><span class="line">print(jihe.pop())</span><br><span class="line">jihe.add(<span class="string">&quot;苍井空&quot;</span>)</span><br><span class="line"></span><br><span class="line">jihe3 = jihe.difference(jihe2)  <span class="comment"># difference() 方法用于返回集合的差集，即返回的集合元素包含在第一个集合中</span></span><br><span class="line"><span class="comment"># 但不包含在第二个集合(方法的参数)中。</span></span><br><span class="line"><span class="comment"># ---------------------------------------------------------#</span></span><br><span class="line"><span class="comment"># jihe.difference_update(jihe2)  # difference_update() 方法用于移除两个集合中都存在的元素</span></span><br><span class="line"><span class="comment"># difference_update() 方法与 difference() 方法的区别在于 difference() 方法返回一个移除相同元素的新集合，</span></span><br><span class="line"><span class="comment"># 而 difference_update() 方法是直接在原来的集合中移除元素，没有返回值。</span></span><br><span class="line">jihe.discard(<span class="string">&quot;胡歌&quot;</span>)  <span class="comment"># discard() 方法用于移除指定的集合元素。</span></span><br><span class="line"><span class="comment"># 该方法不同于 remove() 方法，因为 remove() 方法在移除一个不存在的元素时会发生错误，而 discard() 方法不会</span></span><br><span class="line">jihe.remove(<span class="string">&quot;景天&quot;</span>)  <span class="comment"># remove() 方法用于移除集合中的指定元素。</span></span><br><span class="line">jihe4 = jihe.intersection(jihe2)  <span class="comment"># intersection() 方法用于返回两个或更多集合中都包含的元素，即交集。</span></span><br><span class="line">jihe5 = jihe.isdisjoint(jihe2)  <span class="comment"># isdisjoint() 方法用于判断两个集合是否包含相同的元素，如果没有返回 True，否则返回 False</span></span><br><span class="line">jihe6 = jihe.issubset(jihe2) <span class="comment"># issubset() 方法用于判断集合的所有元素是否都包含在指定集合中，如果是则返回 True，否则返回 False</span></span><br><span class="line">jihe7 = jihe.issuperset(jihe2)  <span class="comment"># issuperset() 方法用于判断指定集合的所有元素是否都包含在原始的集合中，如果是则返回 True，否则返回 False</span></span><br><span class="line">jihe8 = jihe.symmetric_difference(jihe2)  <span class="comment"># symmetric_difference() 方法返回两个集合中不重复的元素集合，即会移除两个集合中都存在的元素</span></span><br><span class="line">jihe.symmetric_difference_update(jihe2)  <span class="comment"># symmetric_difference_update() 方法移除当前集合中在另外一个指定集合相同的元素，</span></span><br><span class="line"><span class="comment"># 并将另外一个指定集合中不同的元素插入到当前集合中。</span></span><br><span class="line">jihe.union(jihe2)  <span class="comment"># union() 方法返回两个集合的并集，即包含了所有集合的元素，重复的元素只会出现一次</span></span><br><span class="line"><span class="comment"># ###</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; # 下面展示两个集合间的运算.</span></span><br><span class="line"><span class="comment"># ...</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a = set(&#x27;abracadabra&#x27;)</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; b = set(&#x27;alacazam&#x27;)</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a</span></span><br><span class="line"><span class="comment"># &#123;&#x27;a&#x27;, &#x27;r&#x27;, &#x27;b&#x27;, &#x27;c&#x27;, &#x27;d&#x27;&#125;</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a - b                              # 集合a中包含而集合b中不包含的元素</span></span><br><span class="line"><span class="comment"># &#123;&#x27;r&#x27;, &#x27;d&#x27;, &#x27;b&#x27;&#125;</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a | b                              # 集合a或b中包含的所有元素</span></span><br><span class="line"><span class="comment"># &#123;&#x27;a&#x27;, &#x27;c&#x27;, &#x27;r&#x27;, &#x27;d&#x27;, &#x27;b&#x27;, &#x27;m&#x27;, &#x27;z&#x27;, &#x27;l&#x27;&#125;</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a &amp; b                              # 集合a和b中都包含了的元素</span></span><br><span class="line"><span class="comment"># &#123;&#x27;a&#x27;, &#x27;c&#x27;&#125;</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a ^ b                              # 不同时包含于a和b的元素</span></span><br><span class="line"><span class="comment"># &#123;&#x27;r&#x27;, &#x27;d&#x27;, &#x27;b&#x27;, &#x27;m&#x27;, &#x27;z&#x27;, &#x27;l&#x27;&#125;</span></span><br><span class="line"><span class="comment"># ###</span></span><br><span class="line"><span class="comment">#类似列表推导式，同样集合支持集合推导式(Set comprehension):</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a = &#123;x for x in &#x27;abracadabra&#x27; if x not in &#x27;abc&#x27;&#125;</span></span><br><span class="line"><span class="comment"># &gt;&gt;&gt; a</span></span><br><span class="line"><span class="comment"># &#123;&#x27;r&#x27;, &#x27;d&#x27;&#125;</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-eval和json的使用"><a href="#Python-eval和json的使用" class="headerlink" title="Python eval和json的使用"></a><strong>Python eval和json的使用</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">a = <span class="string">&#x27;input(&quot;请输入你的命令：&quot;)&#x27;</span></span><br><span class="line"><span class="built_in">eval</span>(a)  		<span class="comment">#可以执行字符串中的命令</span></span><br><span class="line">json.dumps() 	<span class="comment">#对数据进行编码，</span></span><br><span class="line">json.loads() 	<span class="comment">#对数据进行解码。</span></span><br><span class="line"><span class="built_in">repr</span>() 			<span class="comment">#函数将对象转化为供解释器读取的形式</span></span><br><span class="line">json里将数据持久化有两个方法：</span><br><span class="line"><span class="comment">#dumps：将数据转换成为json字符串，不会将数据保存在文件中</span></span><br><span class="line"><span class="comment">#dump：将数据转换成为json字符串的同时保存在指定的文件中</span></span><br><span class="line">json里将数据反序列化两个方法：</span><br><span class="line"><span class="comment">#loads:将json字符串加载成为pyhton里的数据</span></span><br><span class="line"><span class="comment">#load：读取文件，把读取的内容加载成为python里的数据</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-pickle模块的使用"><a href="#Python-pickle模块的使用" class="headerlink" title="Python pickle模块的使用"></a>Python pickle模块的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">pikcle的作用是编码和解码</span><br><span class="line">pikcle的四种方法:</span><br><span class="line">dump    对文件中的内容进行编码的方式操作</span><br><span class="line">load    对文件中的内容进行解码操作</span><br><span class="line">dumps   将内容编码成二进制的方式</span><br><span class="line">loads   将内容从二进制上进行解码</span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">#先进行调用pikcle模块</span></span><br><span class="line"><span class="string"># import pickle</span></span><br><span class="line"><span class="string"># list1=[1,2,3,4]</span></span><br><span class="line"><span class="string"># res=pickle.dumps(list1) #:将列表内容转换成二进制</span></span><br><span class="line"><span class="string"># print(res)</span></span><br><span class="line"><span class="string"># res1=pickle.loads(res)</span></span><br><span class="line"><span class="string"># print(res1)</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">#使用pikcle对文件进行写读操作(二进制)</span></span><br><span class="line"><span class="string">#注意必须要使用二进制的方式进行操作</span></span><br><span class="line"><span class="string"># import  pickle</span></span><br><span class="line"><span class="string"># with open(&quot;yangceshi1.txt&quot;,&quot;wb&quot;) as file1:</span></span><br><span class="line"><span class="string">#     pickle.dump(&quot;天上天下，人来人往&quot;,file1)</span></span><br><span class="line"><span class="string"># with open(&quot;yangceshi1.txt&quot;,&quot;rb&quot;) as file1:</span></span><br><span class="line"><span class="string">#     res=pickle.load(file1)</span></span><br><span class="line"><span class="string">#     print(res)</span></span><br><span class="line"><span class="string"># import pickle# list1=[1,2,3,4]</span></span><br><span class="line"><span class="string"># res=pickle.dumps(list1) #:将列表内容转换成二进制</span></span><br><span class="line"><span class="string"># print(res)# res1=pickle.loads(res)</span></span><br><span class="line"><span class="string"># print(res1)</span></span><br><span class="line"><span class="string">#使用pikcle对文件进行写读操作(二进制)#注意必须要使用二进制的方式进行操作# import  pickle# with open(&quot;yangceshi1.txt&quot;,&quot;wb&quot;) as file1:</span></span><br><span class="line"><span class="string">#     pickle.dump(&quot;天上天下，人来人往&quot;,file1)# with open(&quot;yangceshi1.txt&quot;,&quot;rb&quot;) as file1:</span></span><br><span class="line"><span class="string">#     res=pickle.load(file1)</span></span><br><span class="line"><span class="string">#     print(res)python</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-json和pickle的区别"><a href="#Python-json和pickle的区别" class="headerlink" title="Python json和pickle的区别"></a>Python json和pickle的区别</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">pickle可以在python之间进行交互</span><br><span class="line">json可以实现python与不同开发语言的交互</span><br><span class="line">pickle可以序列化python中的任何数据类型</span><br><span class="line">json只能序列化python中的常归数据类型（列表等）</span><br><span class="line">pickle序列化后的对象不可读</span><br><span class="line">json序列化后的对象是可读的</span><br></pre></td></tr></table></figure>

<p><strong>Python os模块</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">print(os.name)  <span class="comment"># 获取当前操作系统平台</span></span><br><span class="line">print(os.getcwd())  <span class="comment"># 获取当前工作目录</span></span><br><span class="line">print(os.getcwdb())  <span class="comment"># 获取当前工作目录，以Unicode返回</span></span><br><span class="line">print(os.system(<span class="string">&quot;net use&quot;</span>))  <span class="comment"># 运行shell命令</span></span><br><span class="line">print(os.listdir())  <span class="comment"># 获取某个目录下的所有文件名</span></span><br><span class="line"><span class="comment"># os.remove(&quot;ss.html&quot;)  # 删除指定的文件</span></span><br><span class="line">print(os.path.exists(<span class="string">&quot;d:\\&quot;</span>))  <span class="comment"># 检验给出的路径是否存在</span></span><br><span class="line">print(os.path.isfile(<span class="string">&quot;test.py&quot;</span>))  <span class="comment"># 判断是否为文件，若是，返回为真</span></span><br><span class="line">print(os.path.abspath(<span class="string">&quot;test.py&quot;</span>))  <span class="comment"># 返回绝对路径</span></span><br><span class="line">print(os.path.realpath(<span class="string">&quot;test.py&quot;</span>))  <span class="comment"># 返回真实路径</span></span><br><span class="line">print(os.path.isdir(<span class="string">&quot;venv&quot;</span>))  <span class="comment"># 判断是否为目录，若是，返回为真</span></span><br><span class="line">print(os.path.splitext(<span class="string">&quot;test.py&quot;</span>))  <span class="comment"># 分离文件名与扩展名</span></span><br><span class="line">print(os.path.split(<span class="string">r&quot;E:\pycharm\day\test.py&quot;</span>))  <span class="comment"># 把一个路径拆分为目录+文件名的形式</span></span><br><span class="line">print(os.path.join(<span class="string">&quot;dd&quot;</span>, <span class="string">&quot;test.py&quot;</span>))  <span class="comment"># 连接目录与文件名或目录</span></span><br><span class="line">print(os.path.basename(<span class="string">r&quot;E:\pycharm\day\test.py&quot;</span>))  <span class="comment"># 返回文件名</span></span><br><span class="line">print(os.path.dirname(<span class="string">r&quot;E:\pycharm\day\test.py&quot;</span>))  <span class="comment"># 返回路径名</span></span><br><span class="line">os.chdir(<span class="string">&quot;d:\\&quot;</span>)  <span class="comment"># 改变工作目录</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-sys模块"><a href="#Python-sys模块" class="headerlink" title="Python sys模块"></a><strong>Python sys模块</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> sys</span><br><span class="line"></span><br><span class="line">print(sys.argv)  <span class="comment"># 命令行参数List，第一个元素是程序本身路径</span></span><br><span class="line">print(sys.version)  <span class="comment"># 获取python版本信息</span></span><br><span class="line">print(sys.path)  <span class="comment"># 返回模块的搜索路径，初始化时使用PYTHONPATH环境变量的值</span></span><br><span class="line">print(sys.modules)  <span class="comment"># 返回系统导入的模块字段，key是模块名，value是模块</span></span><br><span class="line"><span class="comment"># sys.stdout.write(&#x27;please:&#x27;)</span></span><br><span class="line"><span class="comment"># sys.stdin.readline()[:-1]</span></span><br><span class="line">print(sys.modules.keys())  <span class="comment"># 返回所有已经导入的模块名</span></span><br><span class="line">print(sys.modules.values())  <span class="comment"># 返回所有已经导入的模块</span></span><br><span class="line">print(sys.base_prefix)  <span class="comment"># 返回python解释器路径</span></span><br><span class="line">print(sys.platform)  <span class="comment"># 返回操作系统类型</span></span><br><span class="line">print(<span class="string">&quot;请输入你的名字：&quot;</span>)</span><br><span class="line">name = sys.stdin.readline().strip()</span><br><span class="line">sys.stderr.write(<span class="string">&quot;你的名字是：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(name))</span><br><span class="line"><span class="comment"># 使用 sys.stderr 可以获取标准错误的文件句柄对象，</span></span><br><span class="line"><span class="comment"># 将 sys.stdout 中示例中的 stdout 替换为 stderr 即可。</span></span><br><span class="line">sys.getdefaultencoding()  <span class="comment"># 获取当前系统的编码</span></span><br><span class="line">sys.exit(<span class="number">100</span>)	<span class="comment">#退出码为100</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-time-hashlib-uuid模块"><a href="#Python-time-hashlib-uuid模块" class="headerlink" title="Python time hashlib uuid模块"></a><strong>Python time hashlib uuid模块</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> uuid</span><br><span class="line"><span class="keyword">import</span> hashlib</span><br><span class="line"></span><br><span class="line">localtime = time.localtime(time.time())</span><br><span class="line">localtime = time.asctime(time.localtime(time.time()))  <span class="comment"># 格式化时间</span></span><br><span class="line">print(<span class="string">&quot;本地时间为 :&quot;</span>, localtime)</span><br><span class="line">print(time.perf_counter())  <span class="comment"># 返回系统运行时间</span></span><br><span class="line">print(time.process_time())  <span class="comment"># 返回进程运行时间</span></span><br><span class="line">print(uuid.uuid1())  <span class="comment"># uuid1()——基于时间戳</span></span><br><span class="line">print(uuid.uuid3(uuid.NAMESPACE_DNS, <span class="string">&#x27;testme&#x27;</span>))  <span class="comment"># uuid3()——基于名字和MD5散列值</span></span><br><span class="line">print(uuid.uuid4())  <span class="comment"># uuid4()——基于随机数</span></span><br><span class="line">print(uuid.uuid5(uuid.NAMESPACE_DNS, <span class="string">&#x27;testme&#x27;</span>))  <span class="comment"># uuid5()——基于名字的SHA-1散列值</span></span><br><span class="line"><span class="comment">#######hashlib#########</span></span><br><span class="line">md = hashlib.md5()  <span class="comment"># 将hashlib.md5的算法赋值给md</span></span><br><span class="line">md.update(<span class="string">&quot;你好&quot;</span>.encode(<span class="string">&#x27;utf-8&#x27;</span>))  <span class="comment"># 先将你好这个字符串以utf-8编码转换成bytes(字节)格式，再存入到md变量中，因为update中只能存入bytes(字节)</span></span><br><span class="line">md = md.hexdigest()  <span class="comment"># hexdigest(…) 以16进制形式返回加密内容 digest() 以字符串形式返回</span></span><br><span class="line">print(md)</span><br></pre></td></tr></table></figure>

<h3 id="Python-装饰器及高级用法"><a href="#Python-装饰器及高级用法" class="headerlink" title="Python 装饰器及高级用法"></a><strong>Python 装饰器及高级用法</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义一个装饰器</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">clock_game</span>(<span class="params">fn</span>):</span></span><br><span class="line">    <span class="comment"># clock_game为装饰器名字</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">clock</span>(<span class="params">x, y, *args, **kwargs</span>):</span></span><br><span class="line">        <span class="comment"># if args[0] &gt;= 18:</span></span><br><span class="line">        <span class="comment"># clock = kwargs[&#x27;clock&#x27;]</span></span><br><span class="line">        clock = kwargs.get(<span class="string">&#x27;clock&#x27;</span>, <span class="number">30</span>)</span><br><span class="line">        <span class="comment"># kwargs.get 如果不定义clock，就使用默认值，30</span></span><br><span class="line">        <span class="keyword">if</span> clock &gt;= <span class="number">18</span>:</span><br><span class="line">            fn(x, y)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            print(<span class="string">&quot;Go Out&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> clock</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 以上为装饰器的基本结构</span></span><br><span class="line"></span><br><span class="line"><span class="meta">@clock_game  </span><span class="comment"># 使用的装饰器后 play_game=clock_game(play_game),所以fn等于play_game</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">play_game</span>(<span class="params">name, game</span>):</span></span><br><span class="line">    print(<span class="string">&quot;&#123;&#125;正在玩&#123;&#125;&quot;</span>.<span class="built_in">format</span>(name, game))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">play_game(<span class="string">&quot;张三&quot;</span>, <span class="string">&quot;王者荣耀&quot;</span>, clock=<span class="number">3</span>)</span><br><span class="line"><span class="comment"># 当使用了装饰器后，play_game调用的是clock函数，</span></span><br><span class="line"><span class="comment"># 此时的 张三 对应clock中的x，王者荣耀 对应clock中的y</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#高级装饰器的用法</span></span><br><span class="line">给装饰器加参数</span><br><span class="line">user=<span class="number">6</span></span><br><span class="line">read_permission=<span class="number">4</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">check</span>(<span class="params">x, y</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">handle</span>(<span class="params">fn</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">do</span>():</span></span><br><span class="line">            fn()</span><br><span class="line">        <span class="keyword">return</span> do</span><br><span class="line">    <span class="keyword">return</span> handle</span><br><span class="line"></span><br><span class="line"><span class="meta">@check(<span class="params">user, read_permission</span>)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">read</span>():</span></span><br><span class="line">    print(<span class="string">&quot;我在读取内容&quot;</span>)</span><br><span class="line"></span><br><span class="line">read()</span><br></pre></td></tr></table></figure>

<h3 id="Python-魔法方法"><a href="#Python-魔法方法" class="headerlink" title="Python 魔法方法"></a>Python 魔法方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    print(<span class="string">&quot;name&quot;</span>,__name__)</span><br><span class="line">__name__:当直接运行的时候，值是__main__</span><br><span class="line">如果这个py文件作为一个模块导入的时候，值是文件名</span><br><span class="line">__slots__=()</span><br><span class="line">这个属性直接定义在类里，是一个元组，用来规定对象可以存在的属性</span><br><span class="line">__init__：创建对象时调用</span><br><span class="line">__del__：销毁对象时调用</span><br><span class="line">__repr__:当打印一个对象是会调用这个对象的__str__或者__repr__方法</span><br><span class="line">__str__:如果两个方法都写了，选择__str__</span><br><span class="line">__call__: 使用p() call方法调用</span><br><span class="line">__eq__：调用比较方法</span><br></pre></td></tr></table></figure>

<h3 id="Python-类和对象"><a href="#Python-类和对象" class="headerlink" title="Python 类和对象"></a><strong>Python 类和对象</strong></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">类和对象是面向对象编程的俩个核心概念</span><br><span class="line">类是对一群具有相同特征或者行为的事物的一个统称</span><br><span class="line">特征就是一个变量，在类中称为属性</span><br><span class="line">行为就是一个函数，在类中成为方法</span><br><span class="line">类就是由属性和方法组成的一个抽象概念</span><br><span class="line">定义类：</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">person</span>:</span></span><br><span class="line">    __slots__=(<span class="string">&#x27;name&#x27;</span>,<span class="string">&#x27;age&#x27;</span>)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, age</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line">        self.age = age</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">names</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;我的名字是&#123;&#125;,我的年龄是&#123;&#125;&quot;</span>.<span class="built_in">format</span>(self.name, self.age))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">s1 = person(<span class="string">&quot;wang&quot;</span>, <span class="number">18</span>)</span><br><span class="line">s1.names()</span><br></pre></td></tr></table></figure>

<h3 id="Python-args和-kwargs"><a href="#Python-args和-kwargs" class="headerlink" title="*Python args和**kwargs"></a>*<em>Python <em>args和**kwargs</em></em></h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">打包（pack）：*args是把多个位置参数打包成元组，**kwargs是把多个关键字参数打包成字典。 </span><br><span class="line">拆分（unpack）：*args是把打包了的参数拆成单个的，依次赋值给函数的形参，**kwargs是把字典的键值拆成单个的，依次赋值给函数的形参。</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span>(<span class="params">a,b,*args</span>):</span></span><br><span class="line">    print(<span class="string">&quot;a=&quot;</span>,a)</span><br><span class="line">    print(<span class="string">&quot;b=&quot;</span>,b)</span><br><span class="line">    print(<span class="string">&quot;c=&quot;</span>,args)</span><br><span class="line">foo(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>)</span><br><span class="line">a= <span class="number">1</span></span><br><span class="line">b= <span class="number">2</span></span><br><span class="line">c= (<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">foo</span>(<span class="params">**kwargs</span>):</span></span><br><span class="line">    print(kwargs)</span><br><span class="line">foo(a=<span class="number">1</span>,b=<span class="number">2</span>,c=<span class="number">3</span>)</span><br><span class="line">&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">bar</span>(<span class="params">a, b, c</span>):</span></span><br><span class="line">    print(a,b,c)</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">bar(**&#123;<span class="string">&#x27;a&#x27;</span>: <span class="number">1</span>, <span class="string">&#x27;b&#x27;</span>: <span class="number">2</span>, <span class="string">&#x27;c&#x27;</span>: <span class="number">3</span>&#125;) </span><br><span class="line"><span class="number">1</span> <span class="number">2</span> <span class="number">3</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-类方法和静态方法"><a href="#Python-类方法和静态方法" class="headerlink" title="Python 类方法和静态方法"></a>Python 类方法和静态方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">静态方法：没有用到实列对象的任何属性</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">test</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, age</span>):</span></span><br><span class="line">        self.age = age</span><br><span class="line">        self.name = name</span><br><span class="line"></span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">demo</span>():</span></span><br><span class="line">        print(<span class="string">&quot;hello&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">p1 = test(<span class="string">&quot;张三&quot;</span>, <span class="number">18</span>)</span><br><span class="line">test.demo()</span><br><span class="line">p1.demo()</span><br><span class="line">静态方法使用</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">test</span>:</span></span><br><span class="line"><span class="meta">    @staticmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">demo</span>(<span class="params">a, b</span>):</span></span><br><span class="line">        <span class="keyword">return</span> a + b</span><br><span class="line">print(test.demo(<span class="number">1</span>, <span class="number">4</span>)) </span><br><span class="line">类方法：如果一个方法只用到了类属性，我们可以定义为一个类方法</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">test</span>:</span></span><br><span class="line">    <span class="built_in">type</span> = <span class="string">&quot;demo&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">demo</span>(<span class="params">cls</span>):</span></span><br><span class="line">        print(<span class="string">&quot;类方法&quot;</span>)</span><br><span class="line">        print(cls.<span class="built_in">type</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">test.demo()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><img data-src="C:\Users\11063\AppData\Roaming\Typora\typora-user-images\image-20201025145535288.png" alt="image-20201025145535288"></p>
<h3 id="Python-单例设计模式"><a href="#Python-单例设计模式" class="headerlink" title="Python 单例设计模式"></a>Python 单例设计模式</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">单例设计模式，是通过重写__new__方法，使之只能调用一次内存</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">test</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    __instance = <span class="literal">None</span>  <span class="comment"># 类属性</span></span><br><span class="line">    __is_first = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="meta">    @classmethod</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__new__</span>(<span class="params">cls, *args, **kwargs</span>):</span></span><br><span class="line">        <span class="keyword">if</span> cls.__instance <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">            cls.__instance = <span class="built_in">object</span>.__new__(cls)</span><br><span class="line">            <span class="comment"># 申请内存，创建一个对象，并把对象的类型设置为cls</span></span><br><span class="line">            <span class="comment"># instance = object.__new__(cls)</span></span><br><span class="line">        <span class="keyword">return</span> cls.__instance</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, a, b</span>):</span></span><br><span class="line">        <span class="keyword">if</span> self.__is_first:</span><br><span class="line">            self.a = a</span><br><span class="line">        	self.b = b</span><br><span class="line">       	 	self.__is_first = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">s1 = test(<span class="string">&quot;hh&quot;</span>, <span class="string">&quot;xx&quot;</span>)</span><br><span class="line">s2 = test(<span class="string">&quot;mm&quot;</span>, <span class="string">&quot;tt&quot;</span>)</span><br><span class="line">print(s1 <span class="keyword">is</span> s2)</span><br><span class="line">print(s1.a)</span><br></pre></td></tr></table></figure>

<h3 id="Python-继承"><a href="#Python-继承" class="headerlink" title="Python 继承"></a>Python 继承</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">面向对象的编程有三大特性：；封装，继承，多态</span><br><span class="line">封装：函数是对语句的封装，类是对函数和变量的封装</span><br><span class="line">继承：类和类之间可以手动的建立父子关系，父类的属性和方法，子类可以使用</span><br><span class="line">多态：是一种技巧，提高代码的灵活度</span><br><span class="line">继承特点：如果一个类A继承自类B，由类A创建出来的实例对象，类B都可以直接使用</span><br><span class="line">继承的使用：</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">animal</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, name, age</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line">        self.age = age</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">sleep</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(self.name + <span class="string">&quot;正在睡觉&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">dog</span>(<span class="params">animal</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">bark</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(self.name + <span class="string">&quot;正在叫&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">student</span>(<span class="params">animal</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">study</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(self.name + <span class="string">&quot;正在好好学习&quot;</span>)</span><br><span class="line"><span class="comment">###dog() 调用__new__方法,再调用__init__方法</span></span><br><span class="line"><span class="comment">###dog 里没有__new__方法，会查看父类是否重写__new__方法方法</span></span><br><span class="line"><span class="comment">###父类里也没有重写__new__方法，查看父类的父类，找到objec</span></span><br><span class="line"></span><br><span class="line"><span class="comment">###调用__init__方法，dog类没有实现，会自动找animal类</span></span><br><span class="line">d1=dog(<span class="string">&#x27;大黄&#x27;</span>,<span class="number">3</span>)</span><br><span class="line">print(d1.name)<span class="comment">#父类里定义的属性</span></span><br><span class="line">d1.sleep()<span class="comment">#父类的方法，子类实例对象可以直接使用</span></span><br><span class="line">d1.bark()</span><br><span class="line">d2 = student(<span class="string">&quot;小米&quot;</span>, <span class="number">13</span>)</span><br><span class="line">d2.study()</span><br><span class="line">d2.sleep()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">继承的特点</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">demo_a</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;我是A类里的方法demo_a&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">foo</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;我是A类里的方法foo&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">demo_b</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;我是A类里的方法demo_b&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">foo</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;我是B类里的方法foo&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span>(<span class="params">A, B</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">c = C()</span><br><span class="line">c.demo_a()</span><br><span class="line">c.demo_b()</span><br><span class="line"></span><br><span class="line"><span class="comment"># python里允许多继承，如果有多个同名方法，有一个类属性可以查看调用的顺序</span></span><br><span class="line">c.foo()</span><br><span class="line">print(C.__mro__)  <span class="comment">##(&lt;class &#x27;__main__.C&#x27;&gt;, &lt;class &#x27;__main__.A&#x27;&gt;, &lt;class &#x27;__main__.B&#x27;&gt;, &lt;class &#x27;object&#x27;&gt;)</span></span><br><span class="line"> </span><br><span class="line">python 私有属性的继承特点</span><br><span class="line">自己类里定义的私有方法		对象名._类名.__私有方法名</span><br><span class="line">父类的私有方法，子类没有继承	</span><br><span class="line">可以通过	对象名._父类名.__私有方法名</span><br></pre></td></tr></table></figure>

<h3 id="Python-is和isinstance的使用"><a href="#Python-is和isinstance的使用" class="headerlink" title="Python is和isinstance的使用"></a>Python is和isinstance的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">is</span> 和 <span class="built_in">isinstance</span>的区别 <span class="keyword">and</span> <span class="built_in">issubclass</span></span><br><span class="line">定义一个子类和父类</span><br><span class="line"></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span>(<span class="params">A</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line">　　</span><br><span class="line"></span><br><span class="line"><span class="keyword">is</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line">print(<span class="built_in">type</span>(b) <span class="keyword">is</span> B)</span><br><span class="line"><span class="comment"># 结果: True</span></span><br><span class="line"> </span><br><span class="line">print(<span class="built_in">type</span>(b) <span class="keyword">is</span> A)</span><br><span class="line"><span class="comment"># 结果: False</span></span><br><span class="line"><span class="keyword">is</span> 通过<span class="string">&quot;type&quot;</span>可以判断两个类型是否相等, 只会判断绝对相等, 而不去关心父类是谁.</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="built_in">isinstance</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line">print(<span class="built_in">isinstance</span>(b, B))</span><br><span class="line"><span class="comment">#结果: True</span></span><br><span class="line">print(<span class="built_in">isinstance</span>(b, A))</span><br><span class="line"><span class="comment">#结果: True</span></span><br><span class="line"><span class="built_in">isinstance</span> 会对这个对象进行<span class="string">&quot;刨祖坟&quot;</span>式的判断, 先判断本类, 在判断父类, 一直判断, 直到<span class="built_in">object</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"><span class="built_in">issubclass</span></span><br><span class="line"></span><br><span class="line"><span class="number">1</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line"><span class="number">4</span></span><br><span class="line"><span class="number">5</span></span><br><span class="line"><span class="number">6</span></span><br><span class="line"><span class="number">7</span></span><br><span class="line"><span class="number">8</span></span><br><span class="line"><span class="number">9</span></span><br><span class="line"><span class="number">10</span></span><br><span class="line"><span class="number">11</span></span><br><span class="line"><span class="number">12</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>:</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">B</span>(<span class="params">A</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span>(<span class="params">B</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"> </span><br><span class="line">print(<span class="built_in">issubclass</span>(C, B))</span><br><span class="line"><span class="comment"># 结果: True</span></span><br><span class="line"> </span><br><span class="line">print(<span class="built_in">issubclass</span>(C, A))</span><br><span class="line"><span class="comment"># 结果: True</span></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line">判断一个类是否是另外一个类的子类, 也是<span class="string">&quot;刨祖坟&quot;</span>式的判断</span><br></pre></td></tr></table></figure>

<h3 id="python-子类重写父类方法"><a href="#python-子类重写父类方法" class="headerlink" title="python 子类重写父类方法"></a>python 子类重写父类方法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">子类在父类实现的基础上，又添加自己的新功能</span><br><span class="line"><span class="comment">#调用父类方法的两种方式</span></span><br><span class="line"><span class="number">1.</span>父类名。方法名(self,参数列表)</span><br><span class="line">person.__init__(self,name,age)</span><br><span class="line"><span class="number">2.</span>使用<span class="built_in">super</span>直接调用父类的方法，推荐是用此方法</span><br><span class="line"><span class="built_in">super</span>（student,self）.__init__(name,age)</span><br><span class="line">self.shchool = school</span><br></pre></td></tr></table></figure>

<h3 id="Python-多态的使用"><a href="#Python-多态的使用" class="headerlink" title="Python 多态的使用"></a>Python 多态的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">多态是基于继承，通过子类重写父类的方法，</span><br><span class="line">达到不同的子类对象调用相同的父类方法，得到不同的结果</span><br><span class="line">提高代码的灵活度</span><br></pre></td></tr></table></figure>

<h3 id="Python-文件操作"><a href="#Python-文件操作" class="headerlink" title="Python 文件操作"></a>Python 文件操作</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">filename = <span class="string">&quot;bestand.py&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(filename) <span class="keyword">as</span> f:</span><br><span class="line">    content = f.readlines()</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> content:</span><br><span class="line">	print(line)</span><br><span class="line">代码的第一部分将读取文件内容。 读取的所有行将存储在变量内容中。 第二部分将遍历变量内容中的每一行。</span><br><span class="line"></span><br><span class="line">如果您不想读取换行符<span class="string">&quot;\n&quot;</span>，则可以将语句f.readlines()更改为此：</span><br><span class="line"></span><br><span class="line">content = f.read().splitlines()</span><br><span class="line"></span><br><span class="line">当上面的代码起作用时，我们应该始终测试要打开的文件是否存在。 我们将首先测试文件是否不存在，如果存在，它将读取文件，否则返回错误。 如下面的代码：</span><br><span class="line"><span class="keyword">import</span> os.path</span><br><span class="line"></span><br><span class="line">filename = <span class="string">&quot;bestand.py&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> os.path.isfile(filename):</span><br><span class="line">    print(<span class="string">&#x27;File does not exist.&#x27;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(filename) <span class="keyword">as</span> f:</span><br><span class="line">    content = f.read().splitlines()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> line <span class="keyword">in</span> content:</span><br><span class="line">    print(line)</span><br><span class="line"></span><br><span class="line">    </span><br><span class="line">Python 默认支持写文件，不需要特殊模块。 您可以使用.write()方法以及包含文本数据的参数来写入文件。</span><br><span class="line"></span><br><span class="line">在将数据写入文件之前，请调用<span class="built_in">open</span>(filename, <span class="string">&#x27;w&#x27;</span>)函数，其中filename包含文件名或文件名的路径。</span><br><span class="line">filename = <span class="string">&quot;newfile.txt&quot;</span></span><br><span class="line">myfile = <span class="built_in">open</span>(filename, <span class="string">&#x27;w&#x27;</span>)</span><br><span class="line">myfile.write(<span class="string">&#x27;Written with Python\n&#x27;</span>)</span><br><span class="line">myfile.close()</span><br><span class="line">r	打开文件以供读取</span><br><span class="line">w	打开文件进行写入（将截断文件）</span><br><span class="line">b	二进制更多</span><br><span class="line">r+	打开文件进行读写</span><br><span class="line">a+	打开文件进行读写（附加到结尾）</span><br><span class="line">w+	打开文件进行读写（截断文件）</span><br></pre></td></tr></table></figure>

<h3 id="Python-csv文件的读写"><a href="#Python-csv文件的读写" class="headerlink" title="Python csv文件的读写"></a>Python csv文件的读写</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">写：</span><br><span class="line"><span class="keyword">import</span> csv</span><br><span class="line"></span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;demo.csv&#x27;</span>, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf8&#x27;</span>)</span><br><span class="line">w = csv.writer(file)</span><br><span class="line">w.writerows(</span><br><span class="line">    [</span><br><span class="line">        [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;性别&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>],</span><br><span class="line">        [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;性别&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>],</span><br><span class="line">        [<span class="string">&#x27;张三&#x27;</span>, <span class="string">&#x27;性别&#x27;</span>, <span class="string">&#x27;年龄&#x27;</span>],</span><br><span class="line">    ]</span><br><span class="line">)</span><br><span class="line">读：</span><br><span class="line"><span class="keyword">import</span> csv</span><br><span class="line"></span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&#x27;info.csv&#x27;</span>, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf-8&#x27;</span>, newline=<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">r = csv.reader(file)</span><br><span class="line"><span class="keyword">for</span> data <span class="keyword">in</span> r:</span><br><span class="line">    print(data)</span><br><span class="line">file.close()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="Python-将数据写入到内存"><a href="#Python-将数据写入到内存" class="headerlink" title="Python 将数据写入到内存"></a>Python 将数据写入到内存</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> io <span class="keyword">import</span> StringIO</span><br><span class="line"></span><br><span class="line">s_io = StringIO()</span><br><span class="line">s_io.write(<span class="string">&#x27;hello&#x27;</span>)</span><br><span class="line">s_io.write(<span class="string">&#x27;good&#x27;</span>)</span><br><span class="line">print(s_io.getvalue())</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="Python-序列化和反序列化"><a href="#Python-序列化和反序列化" class="headerlink" title="Python 序列化和反序列化"></a>Python 序列化和反序列化</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#序列化：将数据从内存持久化保存到硬盘的过程</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#反序列化：将数据从硬盘加载到内存的过程</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="python-异常处理"><a href="#python-异常处理" class="headerlink" title="python 异常处理"></a>python 异常处理</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">&lt;语句&gt;        <span class="comment">#运行别的代码</span></span><br><span class="line"><span class="keyword">except</span> &lt;名字&gt;：</span><br><span class="line">&lt;语句&gt;        <span class="comment">#如果在try部份引发了&#x27;name&#x27;异常</span></span><br><span class="line"><span class="keyword">except</span> &lt;名字&gt;，&lt;数据&gt;:</span><br><span class="line">&lt;语句&gt;        <span class="comment">#如果引发了&#x27;name&#x27;异常，获得附加的数据</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">&lt;语句&gt;        <span class="comment">#如果没有异常发生</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-finally和with关键字的使用"><a href="#Python-finally和with关键字的使用" class="headerlink" title="Python finally和with关键字的使用"></a>Python finally和with关键字的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">不论<span class="keyword">try</span>中写文件的过程中是否有异常，<span class="keyword">finally</span>中关闭文件的操作一定会执行。由于<span class="keyword">finally</span>的这个特性，<span class="keyword">finally</span>经常被用来做一些清理工作。</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="built_in">print</span> (<span class="string">&quot;i&quot;</span>)</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">    <span class="built_in">print</span> (<span class="string">&quot;不管上面是否异常，我必须输出&quot;</span>)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line"><span class="keyword">with</span>表达式其实是<span class="keyword">try</span>-<span class="keyword">finally</span>的简写形式。但是又不是全相同。</span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">格式</span></span><br><span class="line"><span class="string">with context [as var]:</span></span><br><span class="line"><span class="string">    pass</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">with 语句实质是上下文管理。</span></span><br><span class="line"><span class="string">1、上下文管理协议。包含方法__enter__() 和 __exit__()，支持该协议对象要实现这两个方法。</span></span><br><span class="line"><span class="string">2、上下文管理器，定义执行with语句时要建立的运行时上下文，负责执行with语句块上下文中的进入与退出操作。</span></span><br><span class="line"><span class="string">3、进入上下文的时候执行__enter__方法，如果设置as var语句，var变量接受__enter__()方法返回值。</span></span><br><span class="line"><span class="string">4、如果运行时发生了异常，就退出上下文管理器。调用管理器__exit__方法。</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">自定义类必须包含上述几个方法才能正确使用<span class="keyword">with</span>关键字。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Mycontex</span>(<span class="params"><span class="built_in">object</span></span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,name</span>):</span></span><br><span class="line">        self.name=name</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__enter__</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(<span class="string">&quot;进入enter&quot;</span>)</span><br><span class="line">        <span class="keyword">return</span> self</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">do_self</span>(<span class="params">self</span>):</span></span><br><span class="line">        print(self.name)</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__exit__</span>(<span class="params">self,exc_type,exc_value,traceback</span>):</span></span><br><span class="line">        print(<span class="string">&quot;退出exit&quot;</span>)</span><br><span class="line">        print(exc_type,exc_value)</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">with</span> Mycontex(<span class="string">&#x27;test&#x27;</span>) <span class="keyword">as</span> mc:</span><br><span class="line">        mc.do_self()</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>Python 自定义异常</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">raise</span>语句</span><br><span class="line">主动抛出异常。</span><br><span class="line">格式：</span><br><span class="line">主动抛出异常终止程序</span><br><span class="line"><span class="keyword">raise</span> 异常名称(‘异常描述’)</span><br><span class="line"></span><br><span class="line"><span class="keyword">raise</span> RuntimeError(<span class="string">&#x27;testError&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#最简单的自定义异常</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">FError</span>(<span class="params">Exception</span>):</span></span><br><span class="line">    <span class="keyword">pass</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">抛出异常、用<span class="keyword">try</span>-<span class="keyword">except</span>抛出</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">raise</span> FError(<span class="string">&quot;自定义异常&quot;</span>)</span><br><span class="line"><span class="keyword">except</span> FError <span class="keyword">as</span> e:</span><br><span class="line">    print(e)</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">在这里给一个简单的自定义异常类模版。</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">CustomError</span>(<span class="params">Exception</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,ErrorInfo</span>):</span></span><br><span class="line">        <span class="built_in">super</span>().__init__(self) <span class="comment">#初始化父类</span></span><br><span class="line">        self.errorinfo=ErrorInfo</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__str__</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="keyword">return</span> self.errorinfo</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="keyword">raise</span> CustomError(<span class="string">&#x27;客户异常&#x27;</span>)</span><br><span class="line">    <span class="keyword">except</span> CustomError <span class="keyword">as</span> e:</span><br><span class="line">        print(e)</span><br></pre></td></tr></table></figure>

<h3 id="Python-可迭代对象和迭代器"><a href="#Python-可迭代对象和迭代器" class="headerlink" title="Python 可迭代对象和迭代器"></a>Python 可迭代对象和迭代器</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> collections.abc <span class="keyword">import</span> Iterable</span><br><span class="line">name = []</span><br><span class="line">print(<span class="built_in">isinstance</span>(name, Iterable))	<span class="comment">#True</span></span><br><span class="line"><span class="comment">##只要重写了__iter__方法，就是一个可迭代对象</span></span><br><span class="line">根据循环的原理可以重写迭代器</span><br><span class="line">每一次<span class="keyword">for</span>循环都会使用__next__方法</span><br><span class="line">d=domo(<span class="number">10</span>)</span><br><span class="line">i=d.__iter__()</span><br><span class="line">i.__next()</span><br><span class="line">i=<span class="built_in">iter</span>(d)<span class="comment">#内置函数 就是调用上面的iter方法</span></span><br></pre></td></tr></table></figure>

<h3 id="Python-生成器的使用"><a href="#Python-生成器的使用" class="headerlink" title="Python 生成器的使用"></a>Python 生成器的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">nums = [i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>)]  <span class="comment"># 列表生成式（推导式）</span></span><br><span class="line">print(nums)		<span class="comment">#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span><br><span class="line">g = (i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>))  <span class="comment"># 得到的结果是生成器</span></span><br><span class="line">print(g)	<span class="comment">#&lt;generator object &lt;genexpr&gt; at 0x01D45450&gt;</span></span><br><span class="line"><span class="keyword">for</span> m <span class="keyword">in</span> g:	<span class="comment">#生成器是一个特殊的迭代器，也可以用在for...in...后面</span></span><br><span class="line">    print(m)</span><br><span class="line"><span class="keyword">yield</span> <span class="comment">#将函数变成生成器    </span></span><br><span class="line">迭代器是一个对象，定义<span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">生成器写法上像一个函数</span></span><br><span class="line"><span class="class"><span class="title">https</span>:</span>//www.runoob.com/python3/python3-iterator-generator.html</span><br></pre></td></tr></table></figure>

<h3 id="Python-socket套接字"><a href="#Python-socket套接字" class="headerlink" title="Python socket套接字"></a>Python socket套接字</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> socket</span><br><span class="line">UDP发送数据</span><br><span class="line"><span class="comment"># 创建socket并连接</span></span><br><span class="line"><span class="comment"># AF_INET:表示这个socket是用来进行网络连接</span></span><br><span class="line"><span class="comment"># SOCK_DGRAM：表示连接是一个udp连接</span></span><br><span class="line">s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line"><span class="comment"># 发送数据</span></span><br><span class="line"><span class="comment"># data:要发送的数据，他是二进制数据</span></span><br><span class="line"><span class="comment"># address:发送给谁 参数是一个元组，元组里有两个元素</span></span><br><span class="line"><span class="comment"># 第0个表示目标的ip地址，第一个表示程序的端口号</span></span><br><span class="line"><span class="comment"># 给47.115.158.105 这台主机的9090端口发送消息</span></span><br><span class="line">s.sendto(<span class="string">&#x27;heloo&#x27;</span>.encode(<span class="string">&#x27;utf-8&#x27;</span>), (<span class="string">&#x27;47.115.158.105&#x27;</span>, <span class="number">9090</span>))</span><br><span class="line">s.close()</span><br><span class="line">UDP接收数据</span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个基于udp的网络socket连接</span></span><br><span class="line">s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)</span><br><span class="line"><span class="comment"># 绑定端口号和ip地址</span></span><br><span class="line">s.bind((<span class="string">&#x27;192.168.3.81&#x27;</span>, <span class="number">9090</span>))</span><br><span class="line"><span class="comment"># recvfrom 接收数据</span></span><br><span class="line">data, addr = s.recvfrom(<span class="number">1024</span>)</span><br><span class="line"><span class="comment"># print(content)</span></span><br><span class="line">print(<span class="string">&#x27;从&#123;&#125;地址端口号接受到了消息，内容是：&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(addr[<span class="number">0</span>], addr[<span class="number">1</span>], data.decode(<span class="string">&#x27;utf8&#x27;</span>)))</span><br><span class="line"><span class="comment"># 接受到的数据是一个元组，元组里有俩个元素</span></span><br><span class="line"><span class="comment"># 第0个元素是接收到的数据，第一个元素是发送方的ip地址和端口号</span></span><br><span class="line">s.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#TCP客户端</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="comment">#基于tcp协议的socket连接</span></span><br><span class="line">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">s.connect((<span class="string">&#x27;192.168.3.95&#x27;</span>,<span class="number">9090</span>))</span><br><span class="line">s.send(<span class="string">&#x27;halou&#x27;</span>.encode(<span class="string">&#x27;utf8&#x27;</span>))</span><br><span class="line">s.close()</span><br><span class="line"><span class="comment">#TCP服务器</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line"><span class="comment"># 基于tcp协议的socket连接</span></span><br><span class="line">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">s.bind((<span class="string">&#x27;192.168.3.95&#x27;</span>, <span class="number">9090</span>))</span><br><span class="line">s.listen(<span class="number">128</span>)  <span class="comment"># 把socket变成一个被动监听的socket</span></span><br><span class="line">client_socket, client_addr = s.accept()</span><br><span class="line"><span class="comment"># 接收客户端的请求,接收到的结果是一个元组，元组里有俩个元素，第0个是socket连接。第1个是客户端的ip地址和端口号</span></span><br><span class="line"><span class="comment"># (&lt;socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=(&#x27;192.168.3.95&#x27;, 9090), raddr=(&#x27;192.168.3.81&#x27;, 27928)&gt;, (&#x27;192.168.3.81&#x27;, 27928))</span></span><br><span class="line">data = client_socket.recv(<span class="number">1024</span>)</span><br><span class="line">print(<span class="string">&#x27;接收到了客户端&#123;&#125;端口号&#123;&#125;发送的数据，内容是:&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(client_addr[<span class="number">0</span>], client_addr[<span class="number">1</span>], data.decode(<span class="string">&#x27;utf8&#x27;</span>)))</span><br><span class="line"></span><br><span class="line">s.close()</span><br><span class="line"></span><br><span class="line"><span class="comment">###文件下载案例####</span></span><br><span class="line"><span class="comment">####服务器</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line">server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">server_socket.bind((<span class="string">&#x27;192.168.3.81&#x27;</span>, <span class="number">9090</span>))</span><br><span class="line">server_socket.listen(<span class="number">128</span>)</span><br><span class="line">client_socket, client_addr = server_socket.accept()</span><br><span class="line">data = client_socket.recv(<span class="number">1024</span>).decode(<span class="string">&#x27;utf8&#x27;</span>)</span><br><span class="line"><span class="keyword">if</span> os.path.isfile(data):</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(data, <span class="string">&#x27;r&#x27;</span>, encoding=<span class="string">&#x27;utf8&#x27;</span>) <span class="keyword">as</span> file:</span><br><span class="line">        content = file.read()</span><br><span class="line">    client_socket.send(content.encode(<span class="string">&#x27;utf8&#x27;</span>))</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    print(<span class="string">&#x27;文件不存在&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">####客户端</span></span><br><span class="line"><span class="keyword">import</span> socket</span><br><span class="line"></span><br><span class="line">s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</span><br><span class="line">s.connect((<span class="string">&#x27;192.168.3.81&#x27;</span>, <span class="number">9090</span>))</span><br><span class="line">file_name = <span class="built_in">input</span>(<span class="string">&#x27;请输入你要下载的文件名：&#x27;</span>)</span><br><span class="line">s.send(file_name.encode(<span class="string">&#x27;utf8&#x27;</span>))</span><br><span class="line">content = s.recv(<span class="number">1024</span>).decode(<span class="string">&#x27;utf8&#x27;</span>)</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(file_name, <span class="string">&#x27;w&#x27;</span>, encoding=<span class="string">&#x27;utf8&#x27;</span>) <span class="keyword">as</span> file:</span><br><span class="line">    file.write(content)</span><br><span class="line">s.close()</span><br></pre></td></tr></table></figure>

<h3 id="Python-多线程"><a href="#Python-多线程" class="headerlink" title="Python 多线程"></a>Python 多线程</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 多线程多任务实现方法</span></span><br><span class="line"><span class="keyword">import</span> threading</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dance</span>():</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">        time.sleep(<span class="number">1</span>)</span><br><span class="line">        print(<span class="string">&quot;dancing&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sing</span>():</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>):</span><br><span class="line">        time.sleep(<span class="number">1</span>)</span><br><span class="line">        print(<span class="string">&quot;singing&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">t1 = threading.Thread(target=dance)</span><br><span class="line">t2 = threading.Thread(target=sing)</span><br><span class="line">t1.start()</span><br><span class="line">t2.start()</span><br><span class="line">创建锁</span><br><span class="line">lock=threading.Lock()</span><br><span class="line">lock.acquire()</span><br><span class="line">lock.release()</span><br><span class="line"></span><br><span class="line">线程间通信 queue</span><br><span class="line">线程名=queue.Queue()</span><br><span class="line">线程使用：</span><br><span class="line">q.put()</span><br><span class="line">q.get()</span><br><span class="line">先进先出</span><br></pre></td></tr></table></figure>

<h3 id="Python-多进程的使用"><a href="#Python-多进程的使用" class="headerlink" title="Python 多进程的使用"></a>Python 多进程的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 多进程的使用</span></span><br><span class="line"><span class="keyword">import</span> multiprocessing, time</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dance</span>():</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(n):</span><br><span class="line">        time.sleep(<span class="number">0.5</span>)</span><br><span class="line">        print(<span class="string">&quot;正在跳舞&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">sing</span>():</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(m):</span><br><span class="line">        time.sleep(<span class="number">0.5</span>)</span><br><span class="line">        print(<span class="string">&quot;正在唱歌&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    <span class="comment"># 创建两个进程</span></span><br><span class="line">    <span class="comment"># target用来表示执行的任务</span></span><br><span class="line">    <span class="comment"># args 用来传参。类型是一个元组</span></span><br><span class="line">    p1 = multiprocessing.Process(target=dance, args=(<span class="number">20</span>,))</span><br><span class="line">    p2 = multiprocessing.Process(target=sing, args=(<span class="number">50</span>,))</span><br><span class="line">    p1.start()</span><br><span class="line">    p2.start()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
    </div>
    
    
    
    
        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.png" alt="王博文 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="王博文 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Pyhton/" rel="tag"><i class="fa fa-tag"></i> Pyhton</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/04/14/hexo+Next%E4%B8%BB%E9%A2%98%E9%85%8D%E7%BD%AE/" rel="prev" title="hexo+Next7 主题配置">
      <i class="fa fa-chevron-left"></i> hexo+Next7 主题配置
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/04/15/hexo%E5%AE%89%E8%A3%85/" rel="next" title="hexo安装">
      hexo安装 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
    
      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>
    
      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%AD%97%E5%85%B8%E7%9A%84%E5%A2%9E%E5%88%A0%E6%94%B9%E6%9F%A5"><span class="nav-text">Python 字典的增删改查</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C"><span class="nav-text">Python 字符串操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="nav-text">Python 随机数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E9%9B%86%E5%90%88%E6%93%8D%E4%BD%9C"><span class="nav-text">Python 集合操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-eval%E5%92%8Cjson%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python eval和json的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-pickle%E6%A8%A1%E5%9D%97%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python pickle模块的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-json%E5%92%8Cpickle%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="nav-text">Python json和pickle的区别</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-sys%E6%A8%A1%E5%9D%97"><span class="nav-text">Python sys模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-time-hashlib-uuid%E6%A8%A1%E5%9D%97"><span class="nav-text">Python time hashlib uuid模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E8%A3%85%E9%A5%B0%E5%99%A8%E5%8F%8A%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95"><span class="nav-text">Python 装饰器及高级用法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E9%AD%94%E6%B3%95%E6%96%B9%E6%B3%95"><span class="nav-text">Python 魔法方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1"><span class="nav-text">Python 类和对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-args%E5%92%8C-kwargs"><span class="nav-text">*Python args和**kwargs</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E7%B1%BB%E6%96%B9%E6%B3%95%E5%92%8C%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="nav-text">Python 类方法和静态方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%8D%95%E4%BE%8B%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F"><span class="nav-text">Python 单例设计模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E7%BB%A7%E6%89%BF"><span class="nav-text">Python 继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-is%E5%92%8Cisinstance%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python is和isinstance的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#python-%E5%AD%90%E7%B1%BB%E9%87%8D%E5%86%99%E7%88%B6%E7%B1%BB%E6%96%B9%E6%B3%95"><span class="nav-text">python 子类重写父类方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%A4%9A%E6%80%81%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python 多态的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"><span class="nav-text">Python 文件操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-csv%E6%96%87%E4%BB%B6%E7%9A%84%E8%AF%BB%E5%86%99"><span class="nav-text">Python csv文件的读写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%B0%86%E6%95%B0%E6%8D%AE%E5%86%99%E5%85%A5%E5%88%B0%E5%86%85%E5%AD%98"><span class="nav-text">Python 将数据写入到内存</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96"><span class="nav-text">Python 序列化和反序列化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#python-%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="nav-text">python 异常处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-finally%E5%92%8Cwith%E5%85%B3%E9%94%AE%E5%AD%97%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python finally和with关键字的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%8F%AF%E8%BF%AD%E4%BB%A3%E5%AF%B9%E8%B1%A1%E5%92%8C%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="nav-text">Python 可迭代对象和迭代器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E7%94%9F%E6%88%90%E5%99%A8%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python 生成器的使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-socket%E5%A5%97%E6%8E%A5%E5%AD%97"><span class="nav-text">Python socket套接字</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="nav-text">Python 多线程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Python-%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">Python 多进程的使用</span></a></li></ol></div>
      </div>
      <!--/noindex-->
    
      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王博文"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">王博文</p>
  <div class="site-description" itemprop="description">你走之后 一个夏季熬成一个秋<br>我的书上你的正楷眉清目秀<br>一字一字宣告我们和平分手</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">6</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cDovL3dwYS5xcS5jb20vbXNncmQ/dj0zJnVpbj0xMTA2MzA4OTc2JnNpdGU9cXEmbWVudT15ZXM=" title="QQ → http:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1106308976&amp;site&#x3D;qq&amp;menu&#x3D;yes"><i class="fab fa-qq fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly93ZWliby5jb20vdS81ODY5MTQyMzg4" title="微博 → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;5869142388"><i class="fab fa-weibo fa-fw"></i></span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9tdXNpYy4xNjMuY29tLyMvcGxheWxpc3Q/aWQ9MTcyOTM3MTAy" title="网抑云 → https:&#x2F;&#x2F;music.163.com&#x2F;#&#x2F;playlist?id&#x3D;172937102"><i class="fa fa-music fa-fw"></i></span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cDovL3d3dy5jYW5naGFpeWl4aWFvLmNu" title="http:&#x2F;&#x2F;www.canghaiyixiao.cn">柠檬一柠檬</span>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
          <script type="text/javascript" charset="utf-8" src="/js/tagcloud.js"></script>
          <script type="text/javascript" charset="utf-8" src="/js/tagcanvas.js"></script>
          <div class="widget-wrap">
            <h3 class="widget-title">光。遇</h3>
            <div id="myCanvasContainer" class="widget tagcloud">
              <canvas width="250" height="250" id="resCanvas" style="width:100%">
                <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Next%E6%A8%A1%E6%9D%BF/" rel="tag">Next模板</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pyautogui/" rel="tag">Pyautogui</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pyhton/" rel="tag">Pyhton</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pyqt5/" rel="tag">Pyqt5</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/Pywinauto/" rel="tag">Pywinauto</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/excel%E6%89%93%E5%BC%80%E5%87%BA%E9%94%99/" rel="tag">excel打开出错</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hexo%E5%8D%9A%E5%AE%A2/" rel="tag">hexo博客</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx-%E8%AF%81%E4%B9%A6-ssl/" rel="tag">nginx 证书 ssl</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E4%B8%BB%E9%A2%98/" rel="tag">主题</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E6%A0%87%E7%AD%BE/" rel="tag">标签</a><span class="tag-list-count">1</span></li></ul>
              </canvas>
            </div>
          </div>
    
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>
    
    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王博文</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">30k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">28 分钟</span>
</div>

 <!-- 网站运行时间的设置 -->
    <span id="timeDate">载入天数...</span>
    <span id="times">载入时分秒...</span>  
    <p><i class="fa fa-heartbeat" aria-hidden="true"></i> 兜兜转转还是遇见你 <i class="fa fa-heartbeat" aria-hidden="true"></i></p>
    <script>
        var now = new Date();
        function createtime() {
            var grt= new Date("03/14/2021 00:00:00");//此处修改你的建站时间或者网站上线时间
            now.setTime(now.getTime()+250);
            days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
            hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
            if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
            mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
            seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
            snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
            document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 ";
            document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    }
    setInterval("createtime()",250);
    </script>
        








      </div>
    </footer>
  </div>

  
  <script size="300" alpha="0.6" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 't4vaIbulwHxiCufxjt8n8aSN-gzGzoHsz',
      appKey     : 'Dig4aM7oEo5TNeDjE1vr23xb',
      placeholder: "",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>


 <!-- 页面点击小红心 -->    <script type="text/javascript" src="/js/clicklove.js"></script>

  </body>
</html>

